package org.song.service.impl;

import cn.hutool.core.lang.Assert;
import org.song.exception.myException.CommonException;
import org.song.mapper.MenuMapper;
import org.song.mapper.MenuRoleMapper;
import org.song.mapper.UserMapper;
import org.song.mapper.UserRoleMapper;
import org.song.model.entity.MenuEntity;
import org.song.model.entity.MenuRoleEntity;
import org.song.model.entity.UserRoleEntity;
import org.song.model.entity.UserEntity;
import org.song.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author song
 * @date 2022/3/10 9:58
 **/
@Service( value = "menuServiceImpl" )
public class MenuServiceImpl implements MenuService {

    @Autowired( required = true )
    @Qualifier( value = "userMapper")
    private UserMapper userMapper;

    @Autowired( required = true )
    @Qualifier( value = "userRoleMapper")
    private UserRoleMapper userRoleMapper;

    @Autowired( required = true )
    @Qualifier( value = "menuRoleMapper")
    private MenuRoleMapper menuRoleMapper;

    @Autowired( required = true )
    @Qualifier( value = "menuMapper" )
    private MenuMapper menuMapper;

    /**
     * 根据用户名获取用户对菜单信息
     * @param username
     * @return
     */
    @Override
    public Set<MenuEntity> getMenuListByUserName( @NonNull String username ) {
        // 根据用户名查询用户
        final UserEntity currentUserEntity = userMapper.getUserByUsername(username);
        Assert.isTrue( Objects.nonNull(currentUserEntity) , () -> new CommonException("getMenuListByUserName () 未找到该用户： " + username) );
        final Example userRoleExample = new Example( UserRoleEntity.class );
        userRoleExample.createCriteria().andEqualTo( "userId" , currentUserEntity.getId() );
        // 根据用户Id查询用户角色
        final List<UserRoleEntity> userRoleEntityList = userRoleMapper.selectByExample(userRoleExample);
        Assert.isTrue( !CollectionUtils.isEmpty( userRoleEntityList ) , () -> new CommonException("getMenuListByUserName () 当前用户未设置角色： " + username) );
        final Set<Long> currentUserRoleList = userRoleEntityList.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toSet());
        final Example menuRoleExample = new Example( MenuRoleEntity.class );
        menuRoleExample.createCriteria().andIn( "roleId" , currentUserRoleList );
        // 根据用户角色Id查询菜单
        final List<MenuRoleEntity> menuRoleEntityList = menuRoleMapper.selectByExample(menuRoleExample);
        Assert.isTrue( !CollectionUtils.isEmpty( menuRoleEntityList ) , () -> new CommonException("getMenuListByUserName () 当前用所属角色未设置菜单： " + username) );
        final Set<Long> currentMenuIdList = menuRoleEntityList.stream().map(MenuRoleEntity::getMenuId).collect(Collectors.toSet());
        Example menuExample = new Example( MenuEntity.class );
        menuExample.createCriteria().andIn( "id" , currentMenuIdList );
        return new HashSet<>( menuMapper.selectByExample( menuExample ) );
    }

}
